package com.ruoyi.file.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.file.pojo.FilePojo;
import com.ruoyi.file.service.FileService;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;


/**
 * @author jpj
 * date 2022/4/7 17:34
 */
@Controller
@RequestMapping("/system/file")
public class FileController extends BaseController {
    @Autowired
    FileService fileService;
    @RequestMapping("/getList")
    @ResponseBody
    public TableDataInfo getList(FilePojo file){
        List<FilePojo> list=fileService.findList(file);
        return getDataTable(list);
    }
    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }
    @RequestMapping()
    public String fileVo(){
        return "system/file/fileList";
    }
    @RequestMapping("/upload")
    public String upload(){
        return "system/file/upload";
    }
    @RequestMapping("/commit")
    @ResponseBody
    public AjaxResult Commit(@RequestParam("file") MultipartFile file){
        if(file.isEmpty()){
            return AjaxResult.error();
        }
        int f=fileService.insert(file);
        System.out.println(f);
        return AjaxResult.success();
    }
    @RequestMapping("/remove/{id}")
    @ResponseBody
    public String download(@PathVariable("id") Integer id,HttpServletResponse response,HttpServletRequest request) throws IOException {
        FilePojo filePojo=fileService.getFile(id);
        String realPath=filePojo.getDownload();
        String fileName=filePojo.getFileName();
        if (fileName != null) {
            //设置文件路径
            File file = new File(realPath,fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);//设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("download success");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }

    //文件下载
    @RequestMapping("/downloadLocal/{id}")
    @ResponseBody
    public AjaxResult downloadLocal(HttpServletResponse response,@PathVariable("id") String id) throws FileNotFoundException {
        FilePojo filePojo=fileService.getFile(Integer.parseInt(id));
        String download=filePojo.getDownload();
        String fileName=filePojo.getFileName();
        // 读到流中
        InputStream inStream = new FileInputStream(download);// 文件的存放路径
        // 设置输出的格式
        response.reset();
        response.setContentType("bin");
        response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        try {
            while ((len = inStream.read(b)) > 0)
                response.getOutputStream().write(b, 0, len);
            inStream.close();
            return success("下载成功");
        } catch (IOException e) {
            e.printStackTrace();
            return success("下载失败");
        }

    }





}
